<?php

	/**
	 * 增加采购需求
	 * QiuXiangCheng
	 */
	class Purchase_AddController extends Controller_NeedLogin{

		/**
		 * 特采数据相关操作
		 * @var Obj
		 */
		public $purchaseModel;

		/**
		 * 初始化
		 */
		public function init() {

			parent::init();
			$this -> purchaseModel = new Purchase_Model_ProductCheck;
		}

		/**
		 * 增加特采数据页面
		 */
		public function indexAction(){

			$this -> smarty -> assign('depts', $this -> getwarehouse());
			$this -> smarty -> display('specialdata_add.php');
		}

		/**
		 * 增加特采数据
		 */
		public function addAction(){

			$post = $this -> _request -> getParams();
			$result = ['status' => 0, 'message' => Helper_Lang::$conf['Err1008']];
			Helper_Array::filterModels($post);
			//Helper_Array::pr($post);
			$post['create_time'] = $post['update_time'] = time();
			$purchase = new Purchase_Model_ProductCheck;
			if($purchase -> add($post)){
				$result['status'] = 1;
				$result['message'] = Helper_Lang::$conf['Suc1001'];
			}
			echo json_encode($result);
		}

		/**
		 * 用于AJAX联动类别搜索插件
		 */
		public function getwarehouse(){

			$warehouseModel = new Base_Model_Warehouse;
			$wares = $warehouseModel -> getWarehouseFilterFields('id, warehouse_name, warehouse_code');
			if(count($wares) == 0){
				return false;
			}
			foreach($wares as $k => $v){
				$realGetWares[$v['id']] = $v;
			}
			unset($wares);
			return $realGetWares;
		}

		/**
		 * 提交之前做的验证 判断是否存在该SKU
		 * @return Json
		 */
		public function beforeaddAction(){

			$post = $this -> _request -> getParams();
			$product = new Base_Model_Product;
			echo json_encode($product -> getProduct($post['goods_sn']));
		}

		/**
		 * 商品的特采数量大于20的时候调用此方法
		 * @return [type] [description]
		 */
		public function proportionAction(){

			$param = $this -> _request -> getParams();
			echo json_encode($this -> purchaseModel -> CheckShelfTime($param['goods_sn']));
			exit;
		}

		public function checkpurchaseAction(){

			$param = $this -> _request -> getParams();
			echo json_encode($this -> purchaseModel -> CheckPropertion($param));
			//Helper_Array::pr();
		}
	}
